<template>
	<view class="container">
		<view class="swiperbox">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item v-for="item in bannerList" :key="item.id">
					<image :src="item.pic" mode=""></image>
				</swiper-item>
			</swiper>
			<view class="iconbox" @tap="collected">

				<uni-icons type="star" size="30" :color="isCollect==true?'red':'#fff'"></uni-icons>
			</view>
		</view>
		<view class="container-c">
			<view class="desc">
				<view class="top">
					<text class="nowprice" v-if="infolist.promotionPrice"> <text class="other">¥</text>
						{{infolist.promotionPrice}} <text class="other">.00</text>
					</text>
					<text v-if="infolist.price" :class="{act:infolist.promotionPrice==null}" class="oldprice"><text
							class="other">¥</text>{{infolist.price}}<text class="other">.00</text></text>
					<text class="tip">没有促销使用原价</text>
				</view>
				<view v-if="infolist.name" class="name">
					{{infolist.name}}
				</view>
				<view v-if="infolist.description" class="bottom">
					{{infolist.description}}
				</view>
			</view>
			<mp-html :tag-style="imgStyle" :content="infolist.detailMobileHtml" />
		</view>
		<view class="bottomtab">
			<uni-icons class="icon" type="headphones" size="24"></uni-icons>
			<text class="addcart" @tap="addCart">添加到购物车</text>
			<text class="nowbuy" @tap="goBuy">立即购买</text>
		</view>
		<!-- 购买商品详情弹框 -->
		<view class="mask" @touchmove.stop.prevent="" @tap="tapMask" v-if="isShow==true">
			<view @tap.stop="stopPp" class="bookinfo">
				<image class="img" :src="skulist[defaultShow].pic" mode=""></image>
				<view class="top">
					<view class="left">
						<view class="title">
							单人沙发
						</view>
						<view class="num">
							库存数量:{{skulist[defaultShow].stock}}
						</view>
					</view>
					<view class="right" @tap="closeCart">
						<uni-icons type="close" size="30"></uni-icons>
					</view>
				</view>
				<view class="choice">
					<view class="title">
						商品选择
					</view>
					<view class="type" @tap="defaultShow=index" :class="{active:defaultShow==index}"
						v-for="(item,index) in skulist" :key="index">
						<view v-for="value in item.spData">
							{{value.key}}:{{value.value}}
						</view>
					</view>
				</view>
				<view class="serve">
					<view class="title">
						特色服务
					</view>
					<view class="top">
						送货上门
					</view>
					<view class="bottom">
						上门安装
					</view>
				</view>
				<view class="line">

				</view>
				<view class="number">
					<text>购买数量</text>
					<view class="right">
						<view @tap="changeNum(false)" class="cut">
							-
						</view>
						<view class="num">
							{{num}}
						</view>
						<view @tap="changeNum(true)" class="add">
							+
						</view>
					</view>
				</view>
				<!-- 立即购买 -->
				<view v-if="type==0" class="btn" @tap="buyNow">
					确定
				</view>
				<!-- 添加购物车 -->
				<view v-if="type==1" class="btn" @tap="confirmAddCar">
					确定
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addCart
	} from "@/api/user/cart/index.js"
	import {
		addPreOrder
	} from "@/api/list/order/index.js";
	import {
		productDetail
	} from "@/api/list/info/index.js";
	import goLogin from "@/mixins/isLogin.js"
	export default {
		data() {
			return {
				imgStyle: {
					img: "width:100%;height:auto;display:block;margin:auto;"
				},
				bannerList: [],
				infolist: {},
				isCollect: false,
				isShow: false,
				index: 0,
				// 商品规格			
				num: 1,
				typeId: "",
				skulist: [],
				spData: [],
				defaultShow: 0,
				type: 0
			};
		},
		mixins: [goLogin],
		onLoad(options) {
			// console.log(options)
			productDetail(options.id).then(res => {
				console.log(res);
				this.bannerList = res.data.product.skuList;
				this.skulist = res.data.product.skuList;
				this.infolist = res.data.product;

				this.skulist.forEach(ele => ele.spData = JSON.parse(ele.spData))
				// console.log(this.skulist,'------------')	
				// 进入页面获取收藏初始状态
				var collectList = uni.getStorageSync("collectList");
				if (collectList.length > 0) {
					var item = collectList.find(ele => ele.id == res.data.product.id);
					this.isCollect = item ? true : false;
				} else {
					this.isCollect = false
				}

				uni.setNavigationBarTitle({
					title: this.infolist.name,
				});
			})
		},
		methods: {
			collected() {
				if (this.goLogin()) {
					var collectList = uni.getStorageSync("collectList");
					if (collectList) { //本地已经有收藏的数据			
						var item = collectList.find(ele => ele.id == this.infolist.id);
						if (item) { //本次点击的已经收藏过就取消收藏	
							var index = collectList.findIndex(ele => ele.id == this.infolist.id);
							collectList.splice(index, 1);
							uni.setStorageSync("collectList", collectList);
							this.isCollect = false;
							uni.showToast({
								title: "取消收藏成功"
							})
						} else { // 本次收藏的之前没有收藏过,就添加收藏
							collectList.push(this.infolist)
							uni.setStorageSync("collectList", collectList)
							this.isCollect = true;
							uni.showToast({
								title: "收藏成功"
							})
						}

					} else { //没有收藏过商品
						var arr = [];
						arr.push(this.infolist);
						uni.setStorageSync("collectList", arr);
						this.isCollect = true;
						uni.showToast({
							title: "收藏成功"
						})
					}
				} else {
					return
				}

			},
			goBuy() {
				this.isShow = true;
				this.type = 0;
			},
			tapMask() {
				this.isShow = false;
			},
			stopPp() {},
			closeCart() {
				this.isShow = false;
			},
			changeNum(bool) {
				if (!bool) {
					if (this.num > 0)
						this.num--
				} else {
					this.num++
				}
			},
			addCart() {
				this.isShow = true;
				this.type = 1;
			},
			confirmAddCar() {
				if (this.goLogin()) {
					if (this.index == 0) {
						this.typeId = this.infolist.skuList[0].id;
					} else if (this.index == 1) {
						this.typeId = this.infolist.skuList[1].id;
					}
					var obj = {
						"productId": this.infolist.id,
						"quantity": this.num,
						"productSkuId": this.typeId
					}
					addCart(obj).then(res => {
						console.log(res)
						console.log(123);
						if(res.success){
							uni.showToast({
								title:"添加购物车成功"
							})
						}
					})
					
				}
			},
			buyNow() {
				if (this.goLogin()) {
					if (this.index == 0) {
						this.typeId = this.infolist.skuList[0].id;
					} else if (this.index == 1) {
						this.typeId = this.infolist.skuList[1].id;
					}
					var obj = {
						"orderItemList": [{
							"productId": this.infolist.id,
							"productQuantity": this.num,
							"productSkuId": this.typeId
						}]

					}
					addPreOrder(obj).then(res => {
						// console.log(res)
						var id = res.data.orderId;
						uni.setStorageSync("infoId", id);

						setTimeout(() => {
							uni.navigateTo({
								url: "/pages/list/order/order?id=" + id
							})
						}, 1000)

					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/login/login"
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	swiper {
		width: 100%;
		height: 634rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.container {
		width: 100%;
		background-color: #f1ece7;
		// height: 100vh;
	}

	.container-c {
		width: 614rpx;
		margin: 0 auto;
	}

	.desc {
		margin: 100rpx 0 200rpx 0;

		.top {
			line-height: 60rpx;

			.act {
				font-size: 60rpx;
				color: #f2270c;
				font-weight: 700;
				text-decoration: none;

				.other {
					font-size: 24rpx;
				}
			}
		}

		.nowprice {
			font-size: 60rpx;
			color: #f2270c;
			font-weight: 700;
		}

		.nowprice .other {
			font-size: 24rpx;
		}

		.oldprice {
			font-size: 24rpx;
			color: #b5b5b5;
			text-decoration: line-through;
			font-weight: 600;
		}

		.tip {
			margin-right: 12rpx;
			padding: 0 12rpx;
			border: 2rpx solid #ff8373;
			border-radius: 5rpx;
			height: 30px;
			color: #f2270c;
			font-size: 20rpx;
			color: #fff;
			background-color: #f2270c;
			border-color: #f2270c;
			margin-left: 10rpx;
		}

		.name {
			color: #262626;
			overflow: hidden;
			font-size: 32rpx;
			font-weight: 700;
		}

		.bottom {
			font-size: 24rpx;
			padding: 36rpx 0 0;
			line-height: 1.3;
			color: #666;
		}
	}

	.swiperbox {
		position: relative;
	}

	.iconbox {
		position: absolute;
		right: 100rpx;
		bottom: -50rpx;
		z-index: 50;
		width: 100rpx;
		height: 100rpx;
		background-color: #354e44;
		border-radius: 50%;
		text-align: center;
		line-height: 100rpx;
	}

	.bottomtab {
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		height: 120rpx;
		padding: 0 60rpx;
	}

	.addcart {
		display: inline-block;
		background-color: #e2e2e2;
		color: #8f8f94;
		width: 238rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		margin-left: 96rpx;
	}

	.nowbuy {
		display: inline-block;
		color: #fff;
		width: 238rpx;
		height: 80rpx;
		background-color: #354e44;
		text-align: center;
		line-height: 80rpx;
	}

	.icon {
		line-height: 120rpx;
	}

	.mask {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .5);
		z-index: 100;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.bookinfo {
		position: relative;
		width: 510rpx;
		height: fit-content;
		background-color: #fff;
		border-radius: 80rpx;
		padding: 60rpx;

		.img {
			position: absolute;
			left: 60rpx;
			top: -60rpx;
			display: block;
			width: 180rpx;
			height: 180rpx;
			border-radius: 20rpx;
		}

		.top {
			display: flex;

			.left {
				position: absolute;
				top: 100rpx;
				right: 60rpx;
				margin-right: 180rpx;

				.title {
					font-size: 28rpx;
				}

				.num {
					color: #b0b0b0;
					font-size: 24rpx;
					line-height: 32rpx;
				}
			}

			.right {
				position: absolute;
				right: 60rpx;
				top: 50rpx;
			}
		}
	}

	.choice {
		margin-top: 150rpx;

		.title {
			margin-bottom: 30rpx;
			font-size: 28rpx;
		}

		.content {
			display: flex;
			background-color: #f2f4f3;
			width: 480rpx;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 26rpx;
			border-radius: 20rpx;
			padding-left: 20rpx;
			margin-bottom: 30rpx;
			border: 4rpx transparent solid;

			&.active {
				border: 4rpx #354e44 solid;
			}
		}
	}



	.serve {
		.title {
			margin-bottom: 10rpx;
			margin-top: 50rpx;
			font-size: 28rpx;
		}

		.top,
		.bottom {
			background-color: #f2f4f3;
			width: 480rpx;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 26rpx;
			border-radius: 20rpx;
			padding-left: 20rpx;
			margin-bottom: 10rpx;
		}

	}

	.number {
		text {
			font-size: 28rpx;
		}

		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 40rpx;

		.right {
			display: flex;
			align-items: center;

			.cut,
			.add {
				width: 60rpx;
				height: 60rpx;
				background-color: #f1ece7;
				border-radius: 50%;
				text-align: center;
				line-height: 60rpx;
			}

			.num {
				width: 90rpx;
				height: 60rpx;
				background-color: #f1ece7;
				border-radius: 40%;
				text-align: center;
				line-height: 60rpx;
				margin-left: 10rpx;
			}

			.add {
				margin-left: 10rpx;
			}
		}
	}

	.btn {
		width: 208rpx;
		height: 80rpx;
		background-color: #354e44;
		border-radius: 28rpx;
		color: #fff;
		text-align: center;
		line-height: 80rpx;
		margin: 0 auto;
		margin-top: 50rpx;
	}

	.line {
		width: 530rpx;
		height: 2rpx;
		background-color: #f2f4f3;
		margin: 0 auto;
		margin-top: 40rpx;
	}

	.type {
		display: flex;
		background-color: #f2f4f3;
		width: 480rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 26rpx;
		border-radius: 20rpx;
		padding-left: 20rpx;
		margin-bottom: 30rpx;
		border: 4rpx transparent solid;

		&.active {
			border: 4rpx #354e44 solid;
		}
	}
</style>
